<template>
  <div class="app-container">
    <!-- 顶部统计 -->
    <analyze></analyze>
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" label-width="68px" size="mini">
      <el-row :gutter="20">
        <el-col :span="4">
          <el-form-item label="单据类型" prop="oddType">
            <el-select v-model="queryParams.oddType" placeholder="请选择">
              <el-option key="1" :value="1" label="销售单"></el-option>
              <el-option key="2" :value="2" label="采购退货单"></el-option>
              <el-option key="2" :value="3" label="销售退换"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="单据编号" prop="oddNo">
            <el-input v-model="queryParams.oddNo" clearable placeholder="请输入" @keyup.enter.native="handleQuery"/>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="客户名称" prop="businessName">
            <el-input v-model="queryParams.params.businessName" clearable placeholder="请输入"
                      @keyup.enter.native="handleQuery"/>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="收款状态" prop="oddStatus">
            <el-select v-model="queryParams.oddStatus" placeholder="请选择">
              <el-option key="0" label="未收款" value="0"></el-option>
              <el-option key="1" label="已收款" value="1"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="单据日期" prop="oddDate">
            <el-date-picker v-model="dateRange" end-placeholder="结束日期" range-separator="-" start-placeholder="开始日期"
                            type="daterange" value-format="yyyy-MM-dd"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label-width="10px">
            <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-hasPermi="['erp:ErpAccountsReceivable:export']" icon="el-icon-download" plain size="mini" type="warning"
                   @click="handleExport">列表导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ErpAccountsReceivableList" row-key="receivableId" @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" width="50" align="center" :reserve-selection="true"/>
      <el-table-column align="center" label="单据编号" sortable prop="oddNo"/>
      <el-table-column align="center" label="供应商/客户" prop="customerOrSupplierName"/>
      <el-table-column align="center" label="单据日期" prop="oddDate" sortable width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.oddDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="单据类型" prop="oddType" sortable width="100">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.oddType == 1">销售单</el-tag>
          <el-tag v-if="scope.row.oddType == 2">采购退货单</el-tag>
          <el-tag v-if="scope.row.oddType == 3">销售退换</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="销售人" prop="nickName" width="120"/>
      <el-table-column align="center" label="应收金额" prop="oddSumMoney" sortable width="120">
        <template slot-scope="scope">
          <span>{{ formattedNumber(scope.row.oddSumMoney) }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="开票日期" prop="invoicingTime" sortable width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.invoicingTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="开票金额" prop="invoicingMoney" sortable width="120">
        <template slot-scope="scope">
          <span>{{ formattedNumber(scope.row.invoicingMoney) }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="收款时间" prop="payTime" sortable width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.payTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="实收金额" prop="oddMoney" sortable width="120">
        <template slot-scope="scope">
          <span>{{ formattedNumber(scope.row.oddMoney) }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="收款方式" prop="payMethod" sortable width="120"/>
      <el-table-column align="center" label="收款状态" prop="oddStatus" sortable width="120">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.oddStatus == 0" type="danger">未收款</el-tag>
          <el-tag v-if="scope.row.oddStatus == 1" type="success">已收款</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作" width="120">
        <template slot-scope="scope">
          <el-button v-if="!scope.row.invoicingTime" v-hasPermi="['erp:ErpAccountsReceivable:invoicing']" size="mini" type="text" @click="handleInvoicing(scope.row)">开票</el-button>
          <el-button v-if="scope.row.oddStatus == 0" v-hasPermi="['erp:ErpAccountsReceivable:receivingPayments']" size="mini" type="text" @click="handleReceivingPayment(scope.row)">收款</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :limit.sync="queryParams.pageSize" :page.sync="queryParams.pageNum" :total="total"
                @pagination="getList"/>
    <!-- 收款 -->
    <el-dialog :close-on-click-modal="false" :visible.sync="isReceivingPaymentOpen" append-to-body title="收款"
               width="400px">
      <el-form :model="form" :rules="rules" label-width="120px">
        <el-form-item label="收款方式" prop="payeeMethod">
          <el-input v-model="form.payMethod" placeholder="请输入收款方式"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isReceivingPaymentOpen = false">取 消</el-button>
        <el-button type="primary" @click="handleUpdateOptions(1)">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 开票 -->
    <el-dialog :close-on-click-modal="false" :visible.sync="isInvoicingOpen" append-to-body title="开票" width="400px">
      <el-form :model="form" :rules="rules" label-width="120px">
        <el-form-item label="开票金额" prop="invoicingMoney">
          <el-input v-model="form.invoicingMoney" placeholder="请输入"/>
        </el-form-item>
        <el-form-item label="开票日期" prop="invoicingTime">
          <el-date-picker v-model="form.invoicingTime" clearable placeholder="请选择开票日期" type="date"
                          value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isInvoicingOpen = false">取 消</el-button>
        <el-button type="primary" @click="handleUpdateOptions(2)">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  invoicingErpAccountsReceivable,
  listErpAccountsReceivable,
  receivingPaymentErpAccountsReceivable
} from "@/api/erp/ErpAccountsReceivable";
import analyze from "./analyze.vue";
import {formattedNumber} from "@/utils/ruoyi";

export default {
  name: "ErpAccountsReceivable",
  components: {
    analyze
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 多选ids
      ids: [],
      // 应收账款表格数据
      ErpAccountsReceivableList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        oddNo: null,
        oddType: null,
        oddStatus: null,
        params: {
          businessName: null,
        }
      },
      // 收款
      isReceivingPaymentOpen: false,
      // 开票
      isInvoicingOpen: false,
      // 日期范围
      dateRange: [],
      form: {},
      rules: []
    };
  },
  created() {
    this.getList();
  },
  methods: {
    formattedNumber,
    /** 查询应收账款列表 */
    getList() {
      this.loading = true;
      listErpAccountsReceivable(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.ErpAccountsReceivableList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.receivableId)
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.queryParams.params.businessName = null;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 收款按钮操作 */
    handleReceivingPayment(row) {
      this.form = row;
      this.isReceivingPaymentOpen = true;
    },
    handleInvoicing(row) {
      this.form = row;
      this.isInvoicingOpen = true;
    },
    // 收款 / 开票
    handleUpdateOptions(type) {
      if (type == 1) {
        receivingPaymentErpAccountsReceivable(this.form).then(res => {
          this.isReceivingPaymentOpen = false;
          this.getList();
          this.$modal.msgSuccess("操作成功");
        })
      }
      if (type == 2) {
        invoicingErpAccountsReceivable(this.form).then(res => {
          this.isInvoicingOpen = false;
          this.getList();
          this.$modal.msgSuccess("操作成功");
        })
      }

    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "erp/ErpAccountsReceivable/export",
        {
          ...this.queryParams,
          exportIds: this.ids?.join(',')
        },
        `应收账款_${new Date().getTime()}.xlsx`
      );
    },
  }

};
</script>
<style scoped>
.el-select {
  width: 100%;
}

.el-range-editor.el-input__inner {
  width: 100% !important;
}
</style>
